package chpt12;

/**
       首个DB测试程序，采用Statement
 */
import java.sql.*;

public class DBbasic1Query {
	public static void main(String[] args) {
		String dirverName = "com.mysql.cj.jdbc.Driver";
		String url = "jdbc:mysql://127.0.0.1:3306/demoDB?serverTimezone=Asia/Chongqing";
		String userName = "root";
		String pwd = "root123";
		String sql = "select * from students";
		Connection con = null;
		try {
			Class.forName(dirverName);  //com.mysql.jdbc.Driver  从JDBC4开始，驱动将自动加载，不需要采用用这句代码来手工加载驱动程序（驱动已通过服务发现机制自动加载）
			con = DriverManager.getConnection(url, 
					userName, pwd);
			Statement stmt = con.createStatement();  
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				System.out.println("id=" + rs.getInt(1) +
						"; 学号=" + rs.getString(2) +
						"; 姓名=" + rs.getString("name") + 
						"; GPA=" + rs.getDouble(4)+
						"; 生日="+rs.getDate(5));
			}
		} catch (SQLException | ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
